//
//  LSTimeSelectView.m
//  LookScore
//
//  Created by wws on 2018/4/11.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSTimeSelectView.h"
#import "LSTimeSelectCell.h"
@interface LSTimeSelectView()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSArray *dataArray;
@end



@implementation LSTimeSelectView{
    
    UIView * _bgView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.layer.cornerRadius = 5;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
    
}

- (instancetype)initWithFrame:(CGRect)frame WithDaraArray:(NSArray *)dataArray{
    
    if (self = [super initWithFrame:frame]) {
        self.dataArray = dataArray;
        [self addContentView];
    }
    return self;
}

- (void)addContentView{
    self.userInteractionEnabled = YES;
    _bgView = [[UIView alloc] initWithFrame: [UIScreen mainScreen].bounds];
    _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    _bgView.alpha = 0;
    _bgView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    tap.delegate = self;
    [_bgView addGestureRecognizer:tap];
    [self addSubview:_bgView];
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = LSTitleWhiteColor;
    view.layer.cornerRadius = 5;
    [_bgView addSubview:view];
    view.sd_layout
    .centerXEqualToView(self)
    .centerYEqualToView(self)
    .widthIs(LSScreenWidth/2)
    .heightIs(240);
    
    [view addSubview:self.tableView];
    
    self.tableView.sd_layout
    .leftEqualToView(view)
    .rightEqualToView(view)
    .topEqualToView(view)
    .bottomEqualToView(view);
    
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth/2, 30)];
    headerView.backgroundColor = LSTitleWhiteColor;
    UILabel * textLabel = [[UILabel alloc] initWithFrame:headerView.frame];
    textLabel.text = @"日期选择";
    textLabel.font = LSFontSize12;
    textLabel.textColor = LSTitleGrayColor2;
    textLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:textLabel];
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = LSLineGrayColor;
    [headerView addSubview:lineView];
    lineView.sd_layout
    .bottomEqualToView(headerView)
    .leftEqualToView(headerView)
    .rightEqualToView(headerView)
    .heightIs(1);
    self.tableView.tableHeaderView = headerView;
    
}

- (void)setSelectTimeStr:(NSString *)selectTimeStr{
    _selectTimeStr = selectTimeStr;
    for (NSInteger i = 0; i< self.dataArray.count; i++) {
        if ([selectTimeStr isEqualToString:self.dataArray[i]]) {
            NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }
    
    
    
}

#pragma mark -- tableViewDelegate
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LSTimeSelectCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if (!cell) {
        cell = [[LSTimeSelectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }
    
    cell.myTitleLabel.text = [self.dataArray[indexPath.row] componentsSeparatedByString:@" "].firstObject;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    self.block(indexPath.row);
    
    [self dismiss];
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 30;
}
//判断手势点在那个view
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:self.tableView]) {
        return NO;
    }
    
    
    return YES;
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview:_bgView];
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 1;
        _bgView.alpha = 1;
    }];
}


- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0;
        _bgView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [_bgView removeFromSuperview];
    }];
    
}

@end
